package likong.bibliothek.pages.autor;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import likong.bibliothek.obj.Autor;

public abstract class AutorFormPanel extends Panel {
	private static final long serialVersionUID = 1L;

	public AutorFormPanel(String id, IModel<Autor> model,
			final String buttonLabel) {
		this(id, model, buttonLabel, true);
	}

	public AutorFormPanel(String id, IModel<Autor> model,
			final String buttonLabel, boolean editable) {
		super(id, model);

		AuthorForm authorForm = new AuthorForm("form", model);

		if (editable) {
			authorForm.add(new TextField<String>("name").setRequired(true));
			authorForm.add(new TextField<String>("vorname").setRequired(true));
		} else {
			authorForm.add(new Label("name").setRenderBodyOnly(true));
			authorForm.add(new Label("vorname").setRenderBodyOnly(true));

		}

		authorForm.add(new FeedbackPanel("feedback"));
		authorForm
				.add(new Button("buttonSubmit", new Model<String>(buttonLabel)));
		authorForm.add(new Link("buttonBack") {
			@Override
			public void onClick() {
				AutorFormPanel.this.onBackButtonClick();
			}
			
		});
		
		add(authorForm);

	}

	protected abstract void onSubmitModelHandle(final IModel<Autor> model);
	
	protected abstract void onBackButtonClick();

	private class AuthorForm extends Form<Autor> {
		public AuthorForm(String id, IModel<Autor> model) {
			super(id, model);
		}

		private static final long serialVersionUID = 1L;

		protected void onSubmit() {
			// *Wie* gespeichert wird, wird hier nicht definiert! Dazu muss die
			// abstrakte äußere Klasse definiert werden.
			onSubmitModelHandle(getModel());
		}
	}
}
